package com.niezhiliang.netty.websocket.spring.boot.demo.service.dto;

import sun.text.resources.cldr.es.FormatData_es_419;

import java.nio.charset.StandardCharsets;

public class CommandLine {

    /*头一个字节表示发送*/
    private int origin;

    /*指明报文长度*/
    private int messageLength;

    /*表示设备类型*/
    private int equipmentType;

    /*zigbee 的mac地址 4个字节*/
    private int[] zigbeeMac = new int[4];

    /*表示modbus相关功能*/
    private int functionType;

    /**
     * 动作命令 8 个字节
     * 第一个字节 表示站号
     * 第二个字节 表示modbus写or读寄存器动作
     * 第三第四个字节 表示寄存器地址
     * 第五第六个字节 表示设备动作指令
     * 第七第八个字节 表示CRC校验
     * */
    private int[] actionCommand = new int[8];

    /*结束符号*/
    private int[] end = new int[2];

    //todo 输出一个字符串
    public String toOneCommandLine(){

        StringBuffer buffer = new StringBuffer();
        buffer.append(origin);
        buffer.append(messageLength);
        buffer.append(equipmentType);
        for (int i = 0; i < zigbeeMac.length; i++) {
            buffer.append(zigbeeMac[i]);
        }
        buffer.append(functionType);
        for (int i = 0; i < actionCommand.length; i++) {
            buffer.append(actionCommand[i]);
        }
        buffer.append("55D5");
        return buffer.toString();
    }

    public static void main(String[] args) {
        CommandLine commandLine = new CommandLine();
        commandLine.origin = 0x80;
        commandLine.messageLength = 0x10;
        commandLine.equipmentType = 0x35;
        commandLine.zigbeeMac = new int[]{0x01,0xff,0x00,0x00};
        commandLine.functionType = 0x05;
        commandLine.actionCommand = new int[]{0x01,0x06,0x11,0xf4,0x00,0x01,0x0c,0xc4};
        String s = commandLine.toOneCommandLine();

    }

}
